package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.List;

import cn.trinea.android.common.util.StringUtils;
import longcaisuyun.longcai.com.fragment.frag_main_dingdanguanli;
import longcaisuyun.longcai.com.mystatic.publicstatic;
import longcaisuyun.longcai.com.net.PostShouFei;
import longcaisuyun.longcai.com.net.PostYiDaoDa;
import longcaisuyun.longcai.com.suyunbean.DingDanGlBean;



/**
 * Created by Administrator on 2016/2/24.
 */
public class DingdanjinxingAdapter extends BaseAdapter {
    Context context;
    ViewHolder vh;
    List<DingDanGlBean> list;
    String type;
    Handler handler;
    private Dialog dialog;

    public DingdanjinxingAdapter(Context context, List<DingDanGlBean> list, String type, Handler handler) {
        this.context = context;
        this.list = list;
        this.type = type;
        this.handler = handler;
    }

    public int getCount() {
        if (list!=null){
            return list.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (list!=null){
            return list.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_dingdan_jinxing, null);
            MyApplication.ScaleScreenHelper.loadView((ViewGroup) convertView);
            vh.t_time = (TextView) convertView.findViewById(R.id.t_time);
            vh.t_zhuangtai = (TextView) convertView.findViewById(R.id.t_zhuangtai);
            vh.t_qidian = (TextView) convertView.findViewById(R.id.t_qidian);
            vh.t_zhongdian = (TextView) convertView.findViewById(R.id.t_zhongdian);
            vh.t_shouru = (TextView) convertView.findViewById(R.id.shouru);
            vh.bt_zhuangtai = (Button) convertView.findViewById(R.id.bt_zhuangtai);
            vh.t_if = (TextView) convertView.findViewById(R.id.tv_shishi);
            vh.r_zhuangtai = (RelativeLayout) convertView.findViewById(R.id.r_zhuangtai);
            vh.lin = (LinearLayout) convertView.findViewById(R.id.lin);
            vh.rl_newText = (RelativeLayout) convertView.findViewById(R.id.rl_newtext);
            vh.bt_fukuan= (Button) convertView.findViewById(R.id.bt_fukuan);
            vh.bt_zhuangtai.setTag(position);
            vh.bt_fukuan.setTag(position);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        vh.t_time.setText(list.get(position).getUsetime());
        vh.t_zhuangtai.setText(iswhat(list.get(position).getCheckinfo()));
        vh.t_qidian.setText(list.get(position).getStart());
        vh.t_zhongdian.setText(list.get(position).getEnd());
        vh.t_shouru.setText("总收入：" + "¥" + list.get(position).getAccount());


        if (list.get(position).getDatetype().equals("1")) {
            vh.rl_newText.setBackgroundColor(Color.parseColor("#69c7e3"));
            vh.t_if.setBackgroundColor(Color.parseColor("#69c7e3"));
            vh.t_if.setText("实时");
            vh.lin.setBackgroundColor(Color.parseColor("#69c7e3"));

        } else {
            vh.rl_newText.setBackgroundColor(Color.parseColor("#43cc82"));
            vh.t_if.setBackgroundColor(Color.parseColor("#43cc82"));
            vh.lin.setBackgroundColor(Color.parseColor("#43cc82"));
            vh.t_if.setText("预约");
        }
        if (list.get(position).getCheckinfo().equals("-10")){
            vh.bt_fukuan.setVisibility(View.INVISIBLE);
        }else{
            vh.bt_fukuan.setVisibility(View.VISIBLE);
            if (list.get(position).getPaystatue().equals("0")){
                vh.bt_fukuan.setText("完成收费");
                vh.bt_fukuan.setEnabled(true);
            }else{
                vh.bt_fukuan.setText("已收费");
                vh.bt_fukuan.setEnabled(false);
                vh.bt_fukuan.setBackgroundColor(Color.parseColor("#e1e5eb"));
            }
        }

        vh.bt_fukuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                diagShouFei(position);
            }
        });
        vh.bt_zhuangtai.setText("已到达");
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(context, DingDanOneActivity.class);
//                intent.putExtra("orderid", list.get(position).getU());
                intent.putExtra("orderid", list.get(position).getId());
                intent.putExtra("type", frag_main_dingdanguanli.type);
                intent.putExtra("checkinfo", list.get(position).getCheckinfo());
                context.startActivity(intent);
            }
        });
        vh.bt_zhuangtai.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (list.get(position).getCheckinfo().equals("3")){
                    DialogDaoDa(list.get(position).getId());
                }else{
                    UtilToast.show(context,"请在送达后确认");
                }
            }
        });
        return convertView;
    }

    private void DialogDaoDa(final String j) {
        LayoutInflater inflater = LayoutInflater.from(context);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialogitem_yzm, null);
        TextView tv_title = (TextView) layout.findViewById(R.id.textView28);
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) layout);
        dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(new BitmapDrawable());
        dialog.setCancelable(false);
        dialog.show();
        dialog.setContentView(layout);
        TextView tv_phone = (TextView) layout.findViewById(R.id.t_phone);
        TextView tv_cancle = (TextView) layout.findViewById(R.id.t_cancle);
        final EditText et_code = (EditText) layout.findViewById(R.id.et_code);
        tv_phone.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                if (StringUtils.isEmpty(et_code.getText().toString())){
                    UtilToast.show(context,"请输入验证码");
                }else{
                    MyApplication.myviewutil.startProgressDialog(context);
                    new PostYiDaoDa(MyApplication.myPreferences.readUid(), j,et_code.getText().toString(), new AsyCallBack<PostYiDaoDa.Info>() {
                        @Override
                        public void onSuccess(String toast, int type, PostYiDaoDa.Info info) throws Exception {
                            super.onSuccess(toast, type, info);
                            if (info.message.equals("1")) {
                                Toast.makeText(context, "确认送货成功", Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                                MyApplication.myviewutil.stopProgressDialog();
                                dialog.dismiss();
                                handler.sendEmptyMessage(0);
                            } else {
                                Toast.makeText(context, "确认送货失败，请重新输入", Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                                MyApplication.myviewutil.stopProgressDialog();
                            }
                        }

                        @Override
                        public void onFail(String toast, int type) throws Exception {
                            super.onFail(toast, type);
                            Toast.makeText(context, "网络不通，请稍后重试", Toast.LENGTH_SHORT).show();
                            dialog.dismiss();
                            MyApplication.myviewutil.stopProgressDialog();
                        }
                    }).execute(context);
                }

            }
        });
        tv_cancle.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                dialog.dismiss();

            }
        });
    }

    class ViewHolder {
        public RelativeLayout rl_newText;
        public TextView tv_shishi;
        public TextView t_time;
        public TextView t_zhuangtai;
        public TextView t_qidian;
        public TextView t_zhongdian;
        public TextView t_shouru;
        public Button bt_zhuangtai,bt_fukuan;
        public TextView t_if;
        public RelativeLayout r_zhuangtai;
        public LinearLayout lin;
    }

    String iswhat(String checkinfo) {
        /*
        * 1已接单 （可以执行送货中操作）
            3送货中（执行已到达操作，已到达）
            4已到达 完成付费
            2已付费，完成付费（完成收费操作）
            -10 已取消，10已完成
        * */
        if (checkinfo.equals("-10")) {
            vh.r_zhuangtai.setBackgroundColor(Color.parseColor("#aaaaaa"));
            vh.bt_zhuangtai.setVisibility(View.GONE);
            return "已取消";
        }
        if (checkinfo.equals("1")) {
            vh.bt_zhuangtai.setVisibility(View.VISIBLE);
            vh.r_zhuangtai.setBackgroundColor(Color.parseColor("#43cc82"));
            return "已接单";
        }
        if (checkinfo.equals("0")) {
            vh.r_zhuangtai.setBackgroundColor(Color.parseColor("#3d9ec3"));
            vh.bt_zhuangtai.setVisibility(View.VISIBLE);
            return "下单中";
        }
        if (checkinfo.equals("3")) {
            vh.bt_zhuangtai.setVisibility(View.VISIBLE);
            vh.r_zhuangtai.setBackgroundColor(Color.parseColor("#3d9dc3"));
            return "送货中";
        }
        if (checkinfo.equals("4")) {
            vh.r_zhuangtai.setBackgroundColor(Color.parseColor("#5f5f5f"));
            vh.bt_zhuangtai.setVisibility(View.GONE);
            return "已送达";
        }
        if (checkinfo.equals("20")) {
            vh.r_zhuangtai.setBackgroundColor(Color.parseColor("#5f5f5f"));
            vh.bt_zhuangtai.setVisibility(View.GONE);
            return "已评价";
        }
        return "";
    }

    public String ifswhat(String checkinfo) {
        if (checkinfo.equals("1")) {
            vh.t_if.setBackgroundColor(Color.parseColor("#43cc82"));
            return "实时";
        }
        if (checkinfo.equals("2")) {
            vh.t_if.setBackgroundColor(Color.parseColor("#69c7e3"));
            return "预约";
        }

        return "";
    }

    String btwhat(String checkinfo) {

        if (checkinfo.equals("-10")) {
            return "已到达";
        }
        return "";
    }
    //完成收费
    public void diagShouFei(final int j) {
        LayoutInflater inflater = LayoutInflater.from(context);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.item_caozuo_phone, null);
        TextView tv_title = (TextView) layout.findViewById(R.id.textView28);
        tv_title.setText("是否收到费用");
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) layout);
        dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(new BitmapDrawable());
        dialog.setCancelable(false);
        dialog.show();
        dialog.setContentView(layout);
        TextView tv_phone = (TextView) layout.findViewById(R.id.t_phone);
        tv_phone.setText("确认");
        TextView tv_cancle = (TextView) layout.findViewById(R.id.t_cancle);
        tv_phone.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                MyApplication.myviewutil.startProgressDialog(context);
                new PostShouFei(MyApplication.myPreferences.readUid(), list.get(j).getId(), new AsyCallBack<PostShouFei.Info>() {
                    @Override
                    public void onSuccess(String toast, int type, PostShouFei.Info info) throws Exception {
                        super.onSuccess(toast, type, info);
                        if (info.message.equals("0")) {
                            Toast.makeText(context, "收费失败", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(context, "收费成功", Toast.LENGTH_SHORT).show();
                            handler.sendEmptyMessage(0);
                        }
                    }
                    @Override
                    public void onFail(String toast, int type) throws Exception {
                        super.onFail(toast, type);
                            Toast.makeText(context, "网络不通，请稍后重试,", Toast.LENGTH_SHORT).show();
                    }
                }).execute(context);

//                final SecretAESDESede secretAESDESede = new SecretAESDESede("123456781234567812345678", "12345678", SecretAESDESede.DESEDE_CBC_PKCS5PADDING);
//                final FinalHttp finalHttp = new FinalHttp();
//                AjaxParams params = new AjaxParams();
//                JSONObject jsonObject = new JSONObject();
//                try {
//                    jsonObject.put("driverid ", MyApplication.myPreferences.readUid());
//                    jsonObject.put("orderid ", list.get(j).getId());
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//                params.put("Keys", secretAESDESede.encrypt(jsonObject.toString()));
//                finalHttp.post("http://120.27.4.115/index.php/Order/collectingpayment", params, new AjaxCallBack<String>() {
//                    public void onStart() {
//                        super.onStart();
////                        MyApplication.myviewutil.startProgressDialog(context, finalHttp, publicUrl.mobilecode);
//                    }
//                    public void onSuccess(String s) {
//                        super.onSuccess(s);
////                        MyApplication.myviewutil.stopProgressDialog();
//                        Log.e("sss",s);
//                    }
//
//                    public void onFailure(Throwable t, int errorNo, String strMsg) {
//                        super.onFailure(t, errorNo, strMsg);
////                        MyApplication.myviewutil.stopProgressDialog();
//                        Toast.makeText(context, "获取验证码失败，请重新获取", Toast.LENGTH_SHORT).show();
//                    }
//                });
                dialog.dismiss();
                MyApplication.myviewutil.stopProgressDialog();
            }
        });
        tv_cancle.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                dialog.dismiss();

            }
        });
    }


    //item下的点击事件
    public void bt_zhuangtai_clickMethod(final int position) {
        if (list.get(position).getCheckinfo().equals("1")) {
            MyApplication.myviewutil.startProgressDialog(context);
            new PostYiDaoDa(MyApplication.myPreferences.readUid(), list.get(position).getId(), "3", new AsyCallBack<PostYiDaoDa.Info>() {

                @Override
                public void onSuccess(String toast, int type, PostYiDaoDa.Info info) throws Exception {
                    super.onSuccess(toast, type, info);

                    if (info.message.equals("1")) {

                        handler.sendEmptyMessage(0);
                    } else {
                        Toast.makeText(context, "操作失败", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFail(String toast, int type) throws Exception {
                    super.onFail(toast, type);
                    Toast.makeText(context, "操作失败", Toast.LENGTH_SHORT).show();
                }
            }).execute(context);
        }
        if (list.get(position).getCheckinfo().equals("3")) {

            new PostYiDaoDa(MyApplication.myPreferences.readUid(), list.get(position).getId(), "4", new AsyCallBack<PostYiDaoDa.Info>() {
                @Override
                public void onSuccess(String toast, int type, PostYiDaoDa.Info info) throws Exception {
                    super.onSuccess(toast, type, info);

                    if (info.message.equals("1")) {
                        publicstatic.i = position;
                        handler.sendEmptyMessage(0);
                    } else {
                        Toast.makeText(context, "操作失败", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFail(String toast, int type) throws Exception {
                    super.onFail(toast, type);
                    Toast.makeText(context, "操作失败", Toast.LENGTH_SHORT).show();

                }
            }).execute(context);
        }

        if (list.get(position).getCheckinfo().equals("2")||list.get(position).getCheckinfo().equals("4")) {
            // MyApplication.myviewutil.startProgressDialog(context);
            new PostYiDaoDa(MyApplication.myPreferences.readUid(), list.get(position).getId(), "10", new AsyCallBack<PostYiDaoDa.Info>() {
                @Override
                public void onSuccess(String toast, int type, PostYiDaoDa.Info info) throws Exception {
                    super.onSuccess(toast, type, info);
                    if (list.size() == 1) {
                        publicstatic.i = position - 1;
                        handler.sendEmptyMessage(1);
                    }
                    if (info.message.equals("1")) {
                        publicstatic.i = position;
                        handler.sendEmptyMessage(0);
                    } else {
                        Toast.makeText(context, "操作失败", Toast.LENGTH_SHORT).show();
                    }
                    // MyApplication.myviewutil.stopProgressDialog();
                }

                @Override
                public void onFail(String toast, int type) throws Exception {
                    super.onFail(toast, type);
                    Toast.makeText(context, "操作失败", Toast.LENGTH_SHORT).show();
                    // MyApplication.myviewutil.stopProgressDialog();
                }
            }).execute(context);
        }
        MyApplication.myviewutil.stopProgressDialog();
    }

    public void notifyDataSetChanged(List<DingDanGlBean> list) {
        this.list=list;
        this.notifyDataSetChanged();
    }

}
